using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using SurfaceCardsBoardLibrary.Interfaces;
using SurfaceCardsBoardLibrary;
using System.Collections.Specialized;
using System.Collections;
using SurfaceCardsGameKit.Games.BasicGame;

namespace SurfaceCardsGameKit
{
    /// <summary>
    /// Interaction logic for SurfaceWindow1.xaml
    /// </summary>
    public partial class Main : SurfaceWindow
    {
        #region Properties

        Game _currentGame = null;

        public Brush GameBackground
        {
            get { return (Brush)GetValue(GameBackgroundProperty); }
            set { SetValue(GameBackgroundProperty, value); }
        }
        public static readonly DependencyProperty GameBackgroundProperty =
            DependencyProperty.Register("GameBackground", typeof(Brush), typeof(Main), new UIPropertyMetadata(null));

        public bool GameCreationPending
        {
            get { return (bool)GetValue(GameCreationPendingProperty); }
            set { SetValue(GameCreationPendingProperty, value); }
        }
        public static readonly DependencyProperty GameCreationPendingProperty =
            DependencyProperty.Register("GameCreationPending", typeof(bool), typeof(Main), new UIPropertyMetadata(false, new PropertyChangedCallback(OnGameCreationPendingChanged)));

        private static void OnGameCreationPendingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if ((bool)args.OldValue == true && (bool)args.NewValue == false)
            {
                Main main = sender as Main;
                main.BoardGrid.Visibility = Visibility.Visible;
                main.GameSelection.Visibility = Visibility.Collapsed;
            }
        }

        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public Main()
        {
            InitializeComponent();
            // Add handlers for Application activation events
            AddActivationHandlers();
        }

        private void ChangeGame()
        {
            this.Dispatcher.BeginInvoke((Action)(() =>
                {
                    GameSelection.SelectedIndex = -1;
                    BoardGrid.Visibility = Visibility.Collapsed;
                    GameSelection.Visibility = Visibility.Visible;

                    this._currentGame.Dispose();
                    this._currentGame = null;
                    this.Board.Clear();
                }), null);
        }

        private void CreateBasicGame()
        {
            this.GameCreationPending = true;

            var game = new BasicGame(this.Board);

            game.ChangeGameCommand = new DelegateCommand(ChangeGame);

            game.CardScatterViewItemStyle = Application.Current.FindResource("CardScatterViewItemStyle") as Style;
            game.DeckScatterViewItemStyle = Application.Current.FindResource("DeckScatterViewItemStyle") as Style;
            game.CircularMenuScatterViewItemStyle = Application.Current.FindResource("CircularMenuScatterViewItemStyle") as Style;

            game.CreateBrushesFor52Cards();
            game.Add52CardsToBoard();
            game.ShuffleCardsPositions(this.Board.Cards.OfType<Card>());

            GameBackground = game.GameBackground;

            this._currentGame = game;

            this.GameCreationPending = false;
        }

        //private void CreateGame()
        //{
        //    this.GameCreationPending = true;

        //    Game g = new Game(this.Board);
        //    g.CardWidth = 100;
        //    g.CardHeight = 200;

        //    g.CardScatterViewItemStyle = Application.Current.FindResource("BaseScatterViewItemStyle") as Style;
        //    g.DeckScatterViewItemStyle = g.CardScatterViewItemStyle;
        //    g.AddCard(new Card { Width = 200, Height = 200, FrontBackground = Brushes.Red, BackBackground = Brushes.Blue });
        //    g.AddCard(new Card { Width = 200, Height = 200, FrontBackground = Brushes.Red, BackBackground = Brushes.Blue });

        //    this.GameCreationPending = false;
        //}

        private void CreateBattle()
        {
            this.GameCreationPending = true;
            Battle game = new Battle(this.Board);

            game.ChangeGameCommand = new DelegateCommand(ChangeGame);

            game.CircularMenuScatterViewItemStyle = Application.Current.FindResource("CircularMenuScatterViewItemStyle") as Style;
            GameBackground = game.GameBackground;

            this._currentGame = game;
            this.GameCreationPending = false;
        }

        private void CreateBlackJack()
        {
            this.GameCreationPending = true;
            BlackJack game = new BlackJack(this.Board);

            game.ChangeGameCommand = new DelegateCommand(ChangeGame);

            game.CircularMenuScatterViewItemStyle = Application.Current.FindResource("CircularMenuScatterViewItemStyle") as Style;

            GameBackground = game.GameBackground;

            this._currentGame = game;
            this.GameCreationPending = false;
        }

        #endregion

        #region Events
        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for Application activation events
            RemoveActivationHandlers();
        }

        /// <summary>
        /// Adds handlers for Application activation events.
        /// </summary>
        private void AddActivationHandlers()
        {
            // Subscribe to surface application activation events
            ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
        }

        /// <summary>
        /// Removes handlers for Application activation events.
        /// </summary>
        private void RemoveActivationHandlers()
        {
            // Unsubscribe from surface application activation events
            ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;
        }

        /// <summary>
        /// This is called when application has been activated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationActivated(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// This is called when application is in preview mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationPreviewed(object sender, EventArgs e)
        {
        }

        /// <summary>
        ///  This is called when application has been deactivated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationDeactivated(object sender, EventArgs e)
        {
        }
        #endregion

        private void SurfaceListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SurfaceListBox s = sender as SurfaceListBox;

            FrameworkElement ui = s.SelectedItem as FrameworkElement;

            if (ui == null)
                return;

            Action action = null;
            switch (ui.Name)
            {
                case "BasicGame":
                    action = () =>
                    {
                        CreateBasicGame();
                    };
                    break;
                case "BlackJack":
                    action = () =>
                    {
                        CreateBlackJack();
                    };
                    break;
                case "Battle":
                    action = () =>
                    {
                        CreateBattle();
                    };
                    break;
            }

            if (action != null)
                this.Dispatcher.BeginInvoke(action, DispatcherPriority.Render, null);
        }
    }
}
